package com.lichuang.dedao.listenBooks.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lichuang.dedao.listenBooks.entity.*;
import com.lichuang.dedao.listenBooks.mapper.ListenBookInterpreterMapper;
import com.lichuang.dedao.listenBooks.mapper.ListenBooksMapper;
import com.lichuang.dedao.listenBooks.mapper.SelectedBooksMapper;
import com.lichuang.dedao.listenBooks.mapper.SelectedListenBookMapper;
import com.lichuang.dedao.listenBooks.util.BeanUtil;
import com.lichuang.dedao.listenBooks.vo.SelectListenBookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 精选书单表 前端控制器
 * </p>
 *
 * @author kangkang
 * @since 2019-07-12
 */
@RestController
@RequestMapping("//selected-books")
public class SelectedBooksController {

    @Autowired
    SelectedBooksMapper selectedBooksMapper;

    @Autowired
    SelectedListenBookMapper selectedListenBookMapper;

    @Autowired
    ListenBooksMapper listenBooksMapper;

    @Autowired
    ListenBookInterpreterMapper listenBookInterpreterMapper;

    @Autowired
    InterpreterBookController interpreterBookController;

    @Autowired
    ListenBookeInterpreterController listenBookeInterpreterController;

    //精选书单中，查所有书单信息
    @RequestMapping("/selectBookList")
    public List<SelectedBooks> selectBookList(){
        List<SelectedBooks> list=selectedBooksMapper.selectList(new QueryWrapper<SelectedBooks>().orderByDesc("createTime"));
        for (SelectedBooks selectedBooks : list) {
            int count=sumCounts(selectedBooks.getSelectedBookId());
            selectedBooks.setSumCount(count);
        }
        return list;
    }

    //根据具体的书单中，查询具体书单的信息
    @RequestMapping("/selectedBySelectId")
    public  List<SelectListenBookVo> selectedBySelectId(@RequestParam("selectedBookId")Integer selectedBookId){
        List<SelectListenBookVo> listenBookVosList=new ArrayList<>();
        SelectListenBookVo selectListenBookVo;

        //查书单和精选书单的中间表
        SelectedListenBook selectedListenBook=new SelectedListenBook();
        selectedListenBook.setSelectedBookId(selectedBookId);
        QueryWrapper wrapper=new QueryWrapper(selectedListenBook);
        List<SelectedListenBook> listenBooks=selectedListenBookMapper.selectList(wrapper);

        for (SelectedListenBook listenBook : listenBooks) {
            if(listenBook.getIsBook()==1) {//等于1就查听书，
                //根据书的id，查他的具体书的信息
                ListenBooks listenBooks1 = listenBooksMapper.selectById(listenBook.getListenBooksId());
                //根据书的id，查具体的解读人信息
                InterpreterBook interpreterBook = interpreterBookController.sInterpreterById(listenBook.getListenBooksId());
                //根据书的id，查具体的书和听书的中间表
                ListenBookInterpreter listenBookInterpreter = listenBookeInterpreterController.sListenInterBook(listenBook.getListenBooksId());
                //赋值
                selectListenBookVo=new SelectListenBookVo();
                selectListenBookVo.setInterTime(listenBookInterpreter.getInterTime());
                selectListenBookVo.setPrice(listenBooks1.getPrice());
                selectListenBookVo.setBookname(listenBooks1.getBookname());
                selectListenBookVo.setMainImg(listenBooks1.getMainImg());
                selectListenBookVo.setName(interpreterBook.getName());
                selectListenBookVo.setListenBooksId(listenBook.getListenBooksId());
                selectListenBookVo.setBookIntroduce(listenBook.getBookIntroduce());
                selectListenBookVo.setSubTitle(listenBook.getSubTitle());
                selectListenBookVo.setMainTitle(listenBook.getMainTitle());
                listenBookVosList.add(selectListenBookVo);
            }else{
                //查电子书的信息，书信息，并添加到listenBookVosList集合中。
            }
        }
        return  listenBookVosList;

    }


    //查具体的书单信息
    @RequestMapping("/selectedBooksOne")
    public SelectedBooks selectedBooksOne(@RequestParam("selectedBookId")Integer selectedBookId) {
        //先查某书单的信息
        SelectedBooks selectedBooks = new SelectedBooks();
        selectedBooks.setSelectedBookId(selectedBookId);
        QueryWrapper queryWrapper = new QueryWrapper(selectedBooks);
        SelectedBooks booksOne = selectedBooksMapper.selectOne(queryWrapper);
        //判断是否匿名。
        Integer recommender = booksOne.getRecommender();
        if (recommender == 1) {
            //不匿名就查,推荐人的名字。//推荐人的这个表，目前没有创建，然后在SelectedBooks类添加个字段，用于存储该推荐人的信息。
        }
        return booksOne;
    }

    //查某书单中的，推荐书的总数。
    public int sumCounts(int SelectedBookId){
        SelectedListenBook selectedListenBook=new SelectedListenBook();
        selectedListenBook.setSelectedBookId(SelectedBookId);
        QueryWrapper queryWrapper=new QueryWrapper(selectedListenBook);
        int sumCount=selectedListenBookMapper.selectCount(queryWrapper);
        return sumCount;
    }

}
